/**
 * Copyright (c) 2013 onionframework.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.onionframework.config;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.Length;
import org.onionframework.core.exception.SystemException;
import org.onionframework.persistence.model.AbstractModel;
import org.onionframework.persistence.model.annotation.DomainModel;
import org.onionframework.persistence.model.annotation.Property;
import org.onionframework.persistence.model.logic.Named;

/**
 * @author allan
 *
 */
@Entity
@Table(name = "T_CONFIG_ITEM")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@DomainModel(name = "系统配置")
public class Config extends AbstractModel implements Named {

	private static final long serialVersionUID = -6439262643335638427L;
	private String name;
	private String description;

	private ConfigValueType valueType = ConfigValueType.STRING;
	private String stringValue;
	private Boolean booleanValue;
	private Integer intValue;
	private String lobValue;

	/**
	 * @return the name
	 */
	@Pattern(regexp = "^[A-Za-z0-9_\\.]+$", message = "配置名称不能为空，且只能包含数字、字母、下划线和点")
	@Column(name="NAME",unique=true)
	@Property(name="配置名称")
	@Length(min = 1, max = 255, message="配置名称长度范围{min}-{max}")
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the value
	 */
	@Transient
	public Object getValue() {
		if (ConfigValueType.LOB.equals(this.getValueType())) {
			return this.getLobValue();
		} else if (ConfigValueType.BOOLEAN.equals(this.getValueType())) {
			return this.getBooleanValue();
		} else if (ConfigValueType.INTEGER.equals(this.getValueType())) {
			return this.getIntValue();
		} else if (ConfigValueType.STRING.equals(this.getValueType())) {
			return this.getStringValue();
		}
		return null;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(Object value) {
		try {
			if (ConfigValueType.LOB.equals(this.getValueType())) {
				this.setLobValue(value.toString());
			} else if (ConfigValueType.BOOLEAN.equals(this.getValueType())) {
				if ("true".equals(value) || "false".equals(value)) {
					this
							.setBooleanValue(Boolean.parseBoolean(value
									.toString()));
				} else {
					throw new SystemException("请输入正确的值");
				}
			} else if (ConfigValueType.INTEGER.equals(this.getValueType())) {
				this.setIntValue(Integer.parseInt(value.toString()));
			} else if (ConfigValueType.STRING.equals(this.getValueType())) {
				if(value!=null && value.toString().length() > 200){
					throw new SystemException("配置值长度范围1-200");
				}
				this.setStringValue(value.toString());
			}
		} catch (Exception e) {
			throw new SystemException("请输入正确的值");
		}
	}

	/**
	 * @return the description
	 */
	@Length(min = 1, max = 255, message="配置说明长度范围{min}-{max}")
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	@NotNull(message = "类型不能为空")
	@Column(name = "TYPE", length = 50)
	@Enumerated(EnumType.STRING)
	public ConfigValueType getValueType() {
		return valueType;
	}

	public void setValueType(ConfigValueType valueType) {
		this.valueType = valueType;
	}

	@Length(min = 1, max = 255, message="字符串值长度范围{min}-{max}")
	public String getStringValue() {
		return stringValue;
	}

	
	public void setStringValue(String stringValue) {
		this.stringValue = stringValue;
	}

	public Boolean getBooleanValue() {
		return booleanValue;
	}

	public void setBooleanValue(Boolean booleanValue) {
		this.booleanValue = booleanValue;
	}

	public Integer getIntValue() {
		return intValue;
	}

	public void setIntValue(Integer intValue) {
		this.intValue = intValue;
	}

	@Lob
	@Type(type = "clobString")
	public String getLobValue() {
		return lobValue;
	}

	public void setLobValue(String lobValue) {
		this.lobValue = lobValue;
	}

}